﻿#include "AppDelegate.h"

#include "HelloWorldScene.h"
#include "Logic.h"
#include "DataRepository.h"

#include "GnsClientFrame.h"
#include "MyWorld01.h"
#include "MyWorld02.h"

USING_NS_CC;

// 建立客户端框架系统
void createClientFrame()
{
	// 创建事件桥梁
	IEventBridge* pIEventBridge = IEventBridge::getInstance();
	assert(pIEventBridge);

	// 创建逻辑模块
	ILogic* pLogic = CLogic::getInstance();

	pLogic->setCircleParam(	1,																	// OnConnect 状态下开始转圈的连接次数
													RETRY_CONNECT_MAX_COUNT/2,		// OnConnect 状态下结束转圈的连接次数
													5,																	// OnReconnectAfterOffline 状态下开始转圈的连接次数
													RETRY_CONNECT_MAX_COUNT			// OnReconnectAfterOffline 状态下结束转圈的连接次数
													); 
	assert(pLogic);

	// 创建数据区
	IDataRepository* pDataRepository = CDataRepository::getInstance();
	assert(pDataRepository);

	//--- 创建框架 ----------------------------
	GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
	assert(pGnsClientFrame);

	{  
		// 设置内存块的参数
		pGnsClientFrame->setMemBlock(CMemBlockPool2::DEF_MEMBLOCK_SIZE, CMemBlockPool2::DEF_INIT_COUNT, CMemBlockPool2::DEF_MAX_COUNT);

		// 设置逻辑模块
		pGnsClientFrame->setLogic(pLogic);

		// 设置数据区模块
		pGnsClientFrame->setDataRepository(pDataRepository);
	}

	int iClientId = 0;
	{	// 第 0 个客户端
		//ClientSocket* pClient = pGnsClientFrame->registClient(iClientId++, "127.0.0.1", 5150);
		ClientSocket* pClient = pGnsClientFrame->registClient(iClientId++, "192.168.1.52", 5150);
		assert(pClient);
		if (pClient)
		{
			// 设置重连信息
			pClient->setReconnectInfo(800, 30);
		}
	}

	if (0)
	{	// 第 1 个客户端
		ClientSocket* pClient = pGnsClientFrame->registClient(iClientId++, "127.0.0.1", 5180);
		assert(pClient);
		if (pClient)
		{
			// 设置重连信息
			pClient->setReconnectInfo(800, 30);
		}
	}

	pGnsClientFrame->init();
	pGnsClientFrame->start();

	int iTmp = 0;
}

// 销毁客户端框架系统
void destroyClientFrame()
{
	// 释放框架
	GnsClientFrame::destoryInstance();

	// 销毁数据区
	CDataRepository::destoryInstance();

	// 销毁逻辑模块
	CLogic::destoryInstance();

	// 销毁事件桥梁
	IEventBridge::destoryInstance();

	int iTmp = 0;
}

AppDelegate::AppDelegate() 
{
	createClientFrame();		// 建立客户端框架系统
}

AppDelegate::~AppDelegate() 
{
	destroyClientFrame();		// 销毁客户端框架系统
}

//if you want a different context,just modify the value of glContextAttrs
//it will takes effect on all platforms
void AppDelegate::initGLContextAttrs()
{
    //set OpenGL context attributions,now can only set six attributions:
    //red,green,blue,alpha,depth,stencil
    GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};

    GLView::setGLContextAttrs(glContextAttrs);
}

bool AppDelegate::applicationDidFinishLaunching() 
{
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
        glview = GLViewImpl::createWithRect("HelloCpp", Rect(0, 0, 960, 640));
        director->setOpenGLView(glview);
    }

    director->getOpenGLView()->setDesignResolutionSize(960, 640, ResolutionPolicy::SHOW_ALL);

    // turn on display FPS
    director->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
	#if VERY_SLOW
		director->setAnimationInterval((float)1.0 / 2);
	#else
		director->setAnimationInterval((float)1.0 / 60);
	#endif

    FileUtils::getInstance()->addSearchPath("res");
	FileUtils::getInstance()->addSearchPath("FishAndBomb");

	GnsClientFrame* pGnsClientFrame = GnsClientFrame::getInstance();
	assert(pGnsClientFrame);

	Scene* scene = nullptr;

	int iType = 2;
	if (0 == iType)
		scene = HelloWorld::createScene();
	else if (1 == iType)
		scene = MyWorld01::createScene();
	else if (2 == iType)
		scene = MyWorld02::createScene();

    // run
    director->runWithScene(scene);

    return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() 
{
    Director::getInstance()->stopAnimation();

    // if you use SimpleAudioEngine, it must be pause
    // SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() 
{
    Director::getInstance()->startAnimation();

    // if you use SimpleAudioEngine, it must resume here
    // SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
